//
//  SYDaiPiGaiView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/25.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYDaiPiGaiView.h"
#import "SYDaiPiGaiTopView.h"
#import "SYYueJuanStudentsView.h"
#import "SYFenShuView.h"
#import "SYYueJuanHtmlTableViewCell.h"
#import "SYYueJuanTuPianTableViewCell.h"


@interface SYDaiPiGaiView ()<SYDaiPiGaiTopViewDelegate,UITableViewDelegate,UITableViewDataSource,SYYueJuanStudentsViewDelegate,SYFenShuViewDelegate>
@property (strong, nonatomic) SYDaiPiGaiTopView *daiPiGaiTopView;//顶部view
@property (strong, nonatomic) SYYueJuanStudentsView *studentsView;//左侧学生名字view
@property (nonatomic, strong) UITableView *curTableView;
@property (strong, nonatomic) SYFenShuView *fenshuView;//分数
@property (strong, nonatomic) NSMutableArray *dataListArr;
@property (strong, nonatomic) SYYueJuanShiTiModel *yjstModel;//卷子
@property (assign, nonatomic) BOOL isShowAns;//是否显示答案
@property (strong, nonatomic) SYNoLabel *noLab;
@end


@implementation SYDaiPiGaiView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initSubView];
        self.dataListArr = [NSMutableArray array];
    }
    return self;
}

- (void)initSubView {
    self.isShowAns = YES;
    [self addSubview:self.daiPiGaiTopView];
    [self.daiPiGaiTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    
    //左侧学生名字
    [self addSubview:self.studentsView];
    
    self.studentsView.delegate = self;
    [self.studentsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.daiPiGaiTopView.mas_bottom);
        make.left.equalTo(self);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.width.mas_equalTo(120);
    }];
    [self addSubview:self.fenshuView];
    
    [self.fenshuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(self);
        make.height.mas_equalTo(160);
        make.left.equalTo(self.studentsView.mas_right).offset(0);
    }];
    
    //题目答案和作答
    [self addSubview:self.curTableView];
    
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.daiPiGaiTopView.mas_bottom);
        make.left.equalTo(self.studentsView.mas_right);
        make.bottom.equalTo(self.fenshuView.mas_top);
        make.right.equalTo(self.mas_right);
    }];
    
    [self.curTableView registerCellWithNIbNames:@[@"SYYueJuanHtmlTableViewCell",@"SYYueJuanTuPianTableViewCell"]];

    self.noLab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self];
    [self addSubview:self.noLab];
    self.noLab.text = @"暂无试题,请获取试题~！";
    [self.noLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(50);
        make.left.right.bottom.equalTo(self);
    }];
    self.noLab.hidden = NO;
    //[self viewContKongJianHidd:NO];
//     [self huoQuShiTi];
}
#pragma mark - 表
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.dataListArr.count == 0) {
        return 0;
    } else {
        return 3;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (self.isShowAns) {
            return 0;
        } else {
            NSString *htmle = self.yjstModel.questionContent;
            
            NSAttributedString *attributed = [self attributedStringWithHTMLString:htmle];
            
            CGFloat height = [attributed  boundingRectWithSize:CGSizeMake(kScreen_WIDTH - 180 , CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
            CGFloat ht = 40;
            if (height < 100) {
                return height + 50+ht;
            } else if (height < 200) {
                return height + 100+ht;
            } else {
                return height + 150+ht;
            }
        }
    } else if (indexPath.section == 1) {

        
//        CGFloat areaWidth = [self.yjstModel.areaWidth integerValue];
//        CGFloat blWidht = areaWidth / (CGRectGetWidth(self.curTableView.frame) - 30 );
        CGFloat areaHeight = [self.yjstModel.areaHeight integerValue];
//        CGFloat height = areaHeight *blWidht;
        return areaHeight + 60;
    } else {
//        CGFloat areaWidth = [self.yjstModel.areaWidth integerValue];
//        CGFloat blWidht = areaWidth / (CGRectGetWidth(self.curTableView.frame) - 30 );
        CGFloat areaHeight = [self.yjstModel.areaHeight integerValue];
//        CGFloat height = areaHeight *blWidht;
        return areaHeight *2 + 30;
    }
    
}
//计算富文本高度
- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString{
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    NSAttributedString *attributed = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
    return attributed;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SYYueJuanHtmlTableViewCell *yjhtmlCell = [tableView dequeueReusableCellWithIdentifier:@"SYYueJuanHtmlTableViewCell" forIndexPath:indexPath];
        
        
        yjhtmlCell.htmlStr = self.yjstModel.questionContent;
        yjhtmlCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return yjhtmlCell;
    } else if (indexPath.section == 1) {
        SYYueJuanHtmlTableViewCell *yjhtmlCell = [tableView dequeueReusableCellWithIdentifier:@"SYYueJuanHtmlTableViewCell" forIndexPath:indexPath];


        yjhtmlCell.yjstModel = self.yjstModel;
        yjhtmlCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return yjhtmlCell;

    } else {
        SYYueJuanTuPianTableViewCell *yjtpCell = [tableView dequeueReusableCellWithIdentifier:@"SYYueJuanTuPianTableViewCell" forIndexPath:indexPath];
        NSString *urlImage = SYStringWithPickFormat(kSYWebViewUrlDefent, self.yjstModel.studentAnswer);
        urlImage = [urlImage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [yjtpCell.daanImageView sd_setImageWithURL:[NSURL URLWithString:urlImage]];
        yjtpCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return yjtpCell;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    UILabel *headerLab = [UILabel titleColour:TITLECOLOR
                                   titleFount:14
                                     pingfang:@"PingFang-SC-Medium"
                                    alignment:NSTextAlignmentLeft
                                    titleLine:14];
    [headerView addSubview:headerLab];
    [headerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView.mas_left).offset(20);
        make.centerY.equalTo(headerView.mas_centerY);
    }];
    if (section == 0) {
        headerLab.text = @"题目";
    } else if (section == 1) {
        headerLab.text = [NSString stringWithFormat:@"答案 题号:%@",self.yjstModel.questionSerial];
    } else {
        headerLab.text = @"学生作答";
    }
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    return headerView;
}



#pragma mark - 获取退回试卷 SYDaiPiGaiTopViewDelegate
- (void)selectBtnType:(SYDaiPiGaiTopViewType)type {
    switch (type) {
        case SYDaiPiGaiTopViewTypeXianShiDan://显示题目
            self.isShowAns =!self.isShowAns;
            [self.curTableView reloadData];
            break;
        case SYDaiPiGaiTopViewTypeZiDong://自动批注分数
            
            break;
        case SYDaiPiGaiTopViewTypeHuoQu://获取试题
            [self huoQuShiTi];
            break;
        case SYDaiPiGaiTopViewTypeTuiHui://退回试题
            [self tuiHuiShiTi];
            break;
        default:
            break;
    }
    
    
}
//获取试题
- (void)huoQuShiTi {
    [MBProgressHUD mj_showMessage:nil toView:self];
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI yj_requestCorrectServicePullQuestionsObj:nil success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        weakSelf.dataListArr = dataListArr;
        if (self.dataListArr.count > 0) {
            
            [weakSelf.studentsView requestDataArr:weakSelf.dataListArr selectIndex:0];
            weakSelf.yjstModel = weakSelf.dataListArr.firstObject;
            weakSelf.fenshuView.yjstModel = weakSelf.dataListArr.firstObject;
            weakSelf.fenshuView.selectIndex = 0;
            [weakSelf.curTableView reloadData];
            [weakSelf viewContKongJianHidd:NO];
            weakSelf.noLab.hidden = YES;
        } else {
            weakSelf.noLab.hidden = NO;
            [weakSelf viewContKongJianHidd:YES];
        }
        
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        weakSelf.noLab.hidden = NO;
        [weakSelf viewContKongJianHidd:YES];
    }];
}
//退回试题
- (void)tuiHuiShiTi {
    NSString *answerIds = nil;
    for (SYYueJuanShiTiModel *yjstModel in self.dataListArr) {
        if (yjstModel.isPiGai != YES) {
            if (answerIds.length == 0) {
                answerIds = yjstModel.answerId;
            } else {
                answerIds =  [NSString stringWithFormat:@"%@,%@",answerIds,yjstModel.answerId];
            }
        }
    }
    
    if (answerIds.length == 0) {
        [MBProgressHUD zr_showError:@"没有试题可以回退哦！"];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI yj_requestCorrectServiceSendBackQuestionIds:answerIds success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD mj_showSuccess:@"退回成功" toView:self];
        [weakSelf.dataListArr removeAllObjects];
        weakSelf.noLab.hidden = NO;
        [weakSelf viewContKongJianHidd:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.message];
    }];
    
}


#pragma mark - 左侧学生
- (void)selectCellModel:(id)model selectIndex:(NSInteger)index{
    self.yjstModel = (SYYueJuanShiTiModel *)model;
    self.fenshuView.yjstModel  = (SYYueJuanShiTiModel *)model;
    self.fenshuView.selectIndex = index;
    [self.curTableView reloadData];
}

#pragma mark - 下边分数批改 SYFenShuViewDelegate
- (void)selectItemBtnIndex:(NSInteger)index fenshuViewType:(SYFenShuViewType)type {
    switch (type) {
        case SYFenShuViewTypeRequest:
        case SYFenShuViewTypeXiayige:
        {
            if (index+1 <=self.dataListArr.count-1) {
                index = index+1;
            } else {
                index = index;
                [MBProgressHUD zr_showError:@"这是最后一题哦~！"];
            }
            
            [self.studentsView requestDataArr:self.dataListArr selectIndex:index];
            
//             [self huoQuShiTi];
        }
            break;
        case SYFenShuViewTypeShangyige://上一题
            [self.studentsView requestDataArr:self.dataListArr selectIndex:index];
            break;
        case SYFenShuViewTypeCheXiao://车削
            
            break;
        default:
            break;
    }
    
    
}

#pragma mark - set
- (SYDaiPiGaiTopView *)daiPiGaiTopView {
    if (!_daiPiGaiTopView) {
        _daiPiGaiTopView = [[NSBundle mainBundle] loadNibNamed:@"SYDaiPiGaiTopView" owner:nil options:nil].firstObject;
        _daiPiGaiTopView.delegate = self;
    }
    return _daiPiGaiTopView;
}
//学生名字
- (SYYueJuanStudentsView *)studentsView {
    if (!_studentsView) {
        _studentsView = [[SYYueJuanStudentsView alloc] init];
    }
    return _studentsView;
}

- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
        //        [_curTableView registerCellWithClassNames:@[@"SYAttendLectureTableViewCell"]];
        // [_curTableView registerCellWithNIbNames:@[@"SYAttendLectureTableViewCell"]];
    }
    return _curTableView;
}

- (SYFenShuView *)fenshuView {
    if (!_fenshuView) {
        _fenshuView = [[NSBundle mainBundle] loadNibNamed:@"SYFenShuView" owner:nil options:nil].firstObject;
        _fenshuView.delegate = self;
//        _fenshuView.backgroundColor = [UIColor yellowColor];
    }
    return _fenshuView;
}

- (void)viewContKongJianHidd:(BOOL)isHidd {
    self.studentsView.hidden = isHidd;
    self.fenshuView.hidden = isHidd;
    self.curTableView.hidden = isHidd;
    self.noLab.hidden = !isHidd;
    if (isHidd == YES) {
        self.backgroundColor = [UIColor whiteColor];
    }
}


@end


















